package com.frx.datastruct.chapterfour;


import com.frx.datastruct.chapterthree.ArrayStack;
import com.frx.datastruct.chapterthree.Stack;

import java.util.Random;

/**
 * @author ：frx
 * @date ：Created in 2019/2/18 21:14
 * @description： 测试用数组实现的栈和用链表实现的栈
 * @modified By：
 * @version: ToDO
 */
public class MainTest {

    private static double testStack(Stack<Integer> stack, int opCount){

        long startTime = System.nanoTime();
        Random random = new Random();
        for(int i = 0;i < opCount;i++){
            stack.push(random.nextInt(Integer.MAX_VALUE));
        }
        for (int i = 0;i < opCount;i++){
            stack.pop();
        }
        long endTime = System.nanoTime();

        return (endTime - startTime) / 1000000000.0;
    }

    /** LinkedListStack和ArrayStack是同一时间复杂度的*/
    public static void main(String[] args){
        int opCount = 10000000;
        ArrayStack<Integer> arrayStack = new ArrayStack<>();
        double arrayTime = testStack(arrayStack,opCount);
        System.out.println("数组实现的栈运行的时间为：" + arrayTime);

        LinkedListStack<Integer> listStack = new LinkedListStack<>();
        double linkedListTime = testStack(listStack,opCount);

        System.out.println("链表实现的栈的运行时间为：" + linkedListTime);
    }
}
